package net.xdclass.common.exception;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.common.JsonData;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 自定义异常处理器
 */
@ControllerAdvice
@Slf4j
public class CustomExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonData Handle(Exception e) {

        if (e instanceof BizException) {
            BizException bizException = (BizException) e;
            log.warn("[业务异常]{}", e);
            return JsonData.buildError(bizException.getCode(), bizException.getMessage());

        } else {
            log.warn("[系统异常]{}", e);
            return JsonData.buildError("全局异常，未知错误");
        }

    }
}
